package com.server;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBElement.GlobalScope;

import org.omg.CORBA.PRIVATE_MEMBER;

import com.gobal.Gobal;
import com.po.Client;

import oracle.net.aso.i;

public class Server80 {

	private static TcpServer tcpServer;

	public static void main(String[] args) throws IOException {

		tcpServer = new TcpServer();

		ServerSocket serverSocket = new ServerSocket(80);
		Socket socket = null;
		while (true) {
			socket = serverSocket.accept();
			OutputStream out = socket.getOutputStream();
			out.write("HTTP/1.0 200 OK\r\n".getBytes());
			out.write("Content-Type:text/html;charset=gbk\r\n".getBytes());
			out.write("\r\n".getBytes());

			InputStream is = socket.getInputStream();
			BufferedReader bhead = new BufferedReader(new InputStreamReader(is));

			String head = bhead.readLine();

			if (head.indexOf("favicon.ico") != -1) {
				out.close();
				socket.close();
				continue;
			}

			InputStream inputStream = new FileInputStream("a.html");
			BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
			String r = "(\\{\\{(.+?)\\}\\})";
			Pattern p = Pattern.compile(r, Pattern.CASE_INSENSITIVE);
			Matcher matcher = null;
			String line = null;
			StringBuffer sb = new StringBuffer();
			// Client c = new Client("127.0.0.1");
			while ((line = br.readLine()) != null) {

				matcher = p.matcher(line);
				boolean result = matcher.find();
				if (result) {
					System.out.println(Gobal.isStart);
					String var = matcher.group(2);
					switch (var) {
					case "a.status":
						if (head.contains("start")) {
							line = matcher.replaceFirst("stop");
						} else if (head.contains("stop")) {
							line = matcher.replaceFirst("start");
						} else {
							line = matcher.replaceFirst("start");
						}
						break;
					case "s.start":
						if (head.contains("start")) {
							if (Gobal.isStart == false) {
								Gobal.isStart = true;

								line = matcher.replaceFirst("close");
								// System.out.println(line);
								new Thread(new Runnable() {
									@Override
									public void run() {
										try {
											tcpServer.getInstance();
											System.out.println("openserver");
											// s.status
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
									}
								}).start();
							}
						} else if (head.contains("stop")) {

							if (Gobal.isStart == true) {
								Gobal.isStart = false;
								line = matcher.replaceFirst("start");
								if (result) {

								}
								tcpServer.closeServer8888();

							}
						} else {
							if (Gobal.isStart == true) {
								line = matcher.replaceFirst("close");
							} else {
								line = matcher.replaceFirst("open");
							}
						}
						break;

					case "c.clientIp":
						// show ip
						line = matcher.replaceFirst("open");
						Set<String> set = Gobal.userpool.keySet();
						StringBuilder stringBuilder = new StringBuilder();
						for (String temp : set) {
							stringBuilder.append("IP:" + temp + " Time:" + Gobal.userpool.get(temp));
						}
						String s = matcher.replaceFirst(stringBuilder.toString());
						line = s;

						break;
					default:
						break;
					}

					result = matcher.find();

					sb.append(line);
				}
			}

			out.write(sb.toString().getBytes());
			out.flush();
			socket.close();

		}

	}
}
